package com.woniuxy.stall.web.controller;


import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.woniuxy.stall.model.Param.SelectStallRentByCertifiedIdParam;
import com.woniuxy.stall.model.form.*;
import com.woniuxy.certified.model.param.LoadAllAccountParam;
import com.woniuxy.core.model.Result;
import com.woniuxy.core.util.JwtUtil;
import com.woniuxy.core.util.PageInfo;
import com.woniuxy.core.util.RedisUtil;
import com.woniuxy.renters.model.form.AddrentersinsertstallForm;
import com.woniuxy.renters.model.param.AddrentersinsertstallParam;
import com.woniuxy.stall.dao.StallCheck;
import com.woniuxy.stall.dao.StallInfo;
import com.woniuxy.stall.service.StallInfoService;
import com.woniuxy.stall.service.impl.dto.StallInfoDTO;
import com.woniuxy.stall.service.param.*;
import com.woniuxy.stall.web.controller.form.AddStallForm;
import com.woniuxy.stall.web.controller.form.CerGetStallPageForm;
import com.woniuxy.stall.web.controller.form.PlatGetStallForm;
import com.woniuxy.stall.web.controller.form.UserGetStallsByTimeForm;
import io.jsonwebtoken.Claims;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 凌玉春
 * @since 2024年01月15日
 */
@RestController
@RequestMapping("/stallInfo")
@Api(tags = "车位详情接口")
public class StallInfoController {
    @Autowired
    private StallInfoService stallInfoService;
    @Autowired
    private RedisUtil redisUtil;

    @ApiOperation("根据ID查找可用车位")
    @GetMapping("/findStallById")
    @ApiImplicitParam(name = "stallInfoId",value = "车位信息id")
    public Result findStallById(Integer stallInfoId) throws Exception {
        StallInfo stallInfo=stallInfoService.findStallById(stallInfoId);
        return Result.getResult(StallInfo.class).setCode(200).setMessage("查找车位成功").setData(stallInfo);
    }
    @ApiOperation("根据ID查找车位编号")
    @GetMapping("/findStallNumById")
    @ApiImplicitParam(name = "stallInfoId",value = "车位信息id")
    public Result findStallNumById(Integer stallInfoId) throws Exception {
        String num=stallInfoService.findStallNumById(stallInfoId);
        return Result.getResult(String.class).setCode(200).setMessage("查找车位编号成功").setData(num);
    }

    @ApiOperation("物业Id查询所有车位")
    @PostMapping("/findStallsByCertifiedId")
    public Result findStallsByCertifiedId(@RequestBody SelectStallForm ssf) throws Exception {
        PageInfo<StallInfo> pageInfo=stallInfoService.findStallsByCertifiedId(ssf);
        return Result.getResult(PageInfo.class).setCode(200).setMessage("根据物业Id查询所有车位成功").setData(pageInfo);
    }

    @ApiOperation("物业新增车位")
    @PostMapping("/certifiedAddStall")
    public Result certifiedAddStall(@RequestBody List<AddStallForm> list, @RequestHeader String token) throws Exception {
        List<AddStallParam> params=new ArrayList<AddStallParam>();
        for (AddStallForm form : list) {
            AddStallParam param = BeanUtil.toBean(form, AddStallParam.class);
            param.setCertifiedInfoId(form.getCertifiedInfoId());
            params.add(param);
        }
        List<Integer> stallInfoIds=stallInfoService.certifiedAddStall(params);
        return Result.getListResult(Integer.class).setMessage("新增车位成功").setData(stallInfoIds);
    }

    @ApiOperation("平台查询待审核车位")
    @GetMapping("/getWaitingCheckStall")
    public Result getWaitingCheckStall(Integer current,Integer size) throws Exception {
        PageInfo<StallInfo> pageInfo=stallInfoService.getWaitingCheckStall(current,size);
        return Result.getResult(PageInfo.class).setCode(200).setMessage("查询所有待审核车位成功").setData(pageInfo);
    }
    @ApiOperation("平台根据物业ID查询待审核车位")
    @GetMapping("/getWaitingCheckStallByCeId")
    @ApiImplicitParam(name = "certifiedId",value = "物业id")
    public Result getWaitingCheckStallByCeId(Integer current,Integer size,Integer certifiedId) throws Exception {
        PageInfo<StallInfo> pageInfo=stallInfoService.getWaitingCheckStallByCeId(current,size,certifiedId);
        return Result.getResult(PageInfo.class).setCode(200).setMessage("查询所有待审核车位成功").setData(pageInfo);
    }
    @ApiOperation("平台审核车位")
    @PostMapping("/checkStall")
    public Result checkStall(@RequestBody Set<Integer> ids) throws Exception {
        stallInfoService.checkStall(ids);
        return Result.getResult().setCode(200).setMessage("审核车位成功");
    }

    @ApiOperation("平台分页查询车位")
    @PostMapping("/platGetStalls")
    public Result platGetStalls(@RequestBody PlatGetStallForm form) throws Exception {
        PlatGetStallParam param = BeanUtil.toBean(form, PlatGetStallParam.class);
        PageInfo<StallInfoDTO> pageInfo=stallInfoService.platGetStalls(param);
        return Result.getResult(PageInfo.class).setCode(200).setMessage("平台分页查询车位成功").setData(pageInfo);
    }

    @ApiOperation("车位主查询我的车位")
    @PostMapping("/renterGetStalls")
    public Result renterGetStalls(@RequestHeader String token) throws Exception {
        String longToken = redisUtil.get(token);
        Claims claims = JwtUtil.parseUserToken(longToken);
        Integer renterId =(Integer) claims.get("id");
        List<StallInfoDTO> list=stallInfoService.renterGetStalls(renterId);
        return Result.getResult(List.class).setCode(200).setMessage("车位主查询我的车位成功").setData(list);
    }

    @ApiOperation("车位主根据ID下架车位")
    @GetMapping("/downStallById")
    @ApiImplicitParam(name = "stallInfoId",value = "车位信息id")
    public Result downStallById(@RequestHeader String token,Integer stallInfoId) throws Exception {
        Claims claims = JwtUtil.parseUserToken(token);
        Integer renterId =(Integer) claims.get("id");
        stallInfoService.downStallById(renterId,stallInfoId);
        return Result.getResult().setMessage("车位下架成功");
    }


    @ApiOperation("车位主根据ID上架车位")
    @PostMapping("/upStallById")
    public Result upStallById(@RequestHeader String token, @RequestBody RenterUpStallByIdForm form) throws Exception {
        System.out.println(form);
        Claims claims = JwtUtil.parseUserToken(token);
        Integer renterId =(Integer) claims.get("id");
        RenterUpStallByIdParam param = BeanUtil.toBean(form, RenterUpStallByIdParam.class);
        param.setRenterInfoId(renterId);
        stallInfoService.upStallById(param);
        return Result.getResult().setMessage("车位上架成功");
    }

    @ApiOperation("物业根据ID下架车位")
    @GetMapping("/cerDownStallById")
    @ApiImplicitParam(name = "stallInfoId",value = "车位信息id")
    public Result cerDownStallById(@RequestHeader String token,Integer stallInfoId) throws Exception {
        Claims claims = JwtUtil.parseAdminToken(token);
        Integer cerAccountId =(Integer) claims.get("id");
        stallInfoService.cerDownStallById(cerAccountId,stallInfoId);
        return Result.getResult().setMessage("下架车位成功");
    }
    @ApiOperation("物业根据ID上架车位")
    @PostMapping("/cerDownStallById")
    public Result cerUpStallById(@RequestHeader String token,@RequestBody CerUpStallByIdForm form) throws Exception {
        System.out.println(form);
        Claims claims = JwtUtil.parseAdminToken(token);
        Integer cerAccountId =(Integer) claims.get("id");
        CerUpStallByIdParam param = BeanUtil.toBean(form, CerUpStallByIdParam.class);
        param.setCerAccountId(cerAccountId);
        stallInfoService.cerUpStallById(param);
        return Result.getResult().setMessage("上架车位成功");
    }

    @ApiOperation("物业分页查询车位")
    @GetMapping("/cerGetStallPage")
    public Result cerGetStallPage(@RequestHeader String token, CerGetStallPageForm form) throws Exception {
        String longToken = redisUtil.get(token);
        Claims claims = JwtUtil.parseUserToken(longToken);
        Integer cerAccountId =(Integer) claims.get("id");
        CerGetStallPageParam param = BeanUtil.toBean(form, CerGetStallPageParam.class);
        param.setCertifiedAccountId(cerAccountId);
        PageInfo<StallInfoDTO> pageInfo=stallInfoService.cerGetStallPage(param);
        return Result.getResult(PageInfo.class).setCode(200).setMessage("物业分页查询车位成功").setData(pageInfo);
    }
    @ApiOperation("出租方根据车位状态查询车位")
    @GetMapping("/renterGetStallByState")
    @ApiImplicitParam(name = "stateId",value = "车位状态id")
    public Result renterGetStallByState(@RequestHeader String token, Integer stateId) throws Exception {
        String longToken = redisUtil.get(token);
        Claims claims = JwtUtil.parseUserToken(longToken);
        Integer renterId =(Integer) claims.get("id");
        List<StallInfoDTO> list=stallInfoService.renterGetStallByState(renterId,stateId);
        return Result.getListResult(StallInfoDTO.class).setCode(200).setMessage("出租方根据车位状态查询车位成功").setData(list);
    }

    @ApiOperation("出租方根据时间段查询空闲车位")
    @GetMapping("/renterGetFreeStallByTime")
    @ApiImplicitParams({@ApiImplicitParam(name = "startTime",value = "开始时间"),@ApiImplicitParam(name = "endTime",value = "结束时间")})
    public Result renterGetFreeStallByTime(@RequestHeader String token, String startTime,String endTime) throws Exception {
        String longToken = redisUtil.get(token);
        Claims claims = JwtUtil.parseUserToken(longToken);
        Integer renterId =(Integer) claims.get("id");
        List<StallInfoDTO> list=stallInfoService.renterGetFreeStallByTime(renterId,startTime,endTime);
        return Result.getListResult(StallInfoDTO.class).setCode(200).setMessage("出租方根据车位状态查询车位成功").setData(list);
    }


    @ApiOperation("出租客添加车位")
    @PostMapping("/rentersinsertstall")
    public Result rentersinsertstall(@RequestBody AddrentersinsertstallForm form, @RequestHeader String token) throws IOException {
        String id = JwtUtil.parseUserToken(token).get("id").toString();
        System.out.println(id);
        AddrentersinsertstallParam param = BeanUtil.toBean(form, AddrentersinsertstallParam.class);
        param.setUserId(Integer.valueOf(id));
        stallInfoService.rentersinsertstall(param);
        return Result.getResult(PageInfo.class).setCode(200).setMessage("出租客添加车位成功,等待物业审核通过").setData(null);
    }

    @ApiOperation("出租客上传购买车位凭证")
    @PostMapping("/uploadImage")
    public Result<String> uploadImage(MultipartFile image) throws IOException {
        String url = String.valueOf(stallInfoService.uploadImage(image));
        return Result.getResult(String.class).setMessage("出租客上传购买车位凭证成功").setData(url);
    }
    
    @ApiOperation("出租客查询自己的车位")
    @PostMapping("/loadAllstall")
    public Result <List> loadAllstall(@RequestHeader String token){
        String id = JwtUtil.parseUserToken(token).get("id").toString();
        System.out.println("LLLLLLLLLLLLLLLLLLLLLLL");
        System.out.println(id);
        List<StallCheck> stallCheck = stallInfoService.loadAllstall(id);
        return Result.getResult(List.class).setMessage("出租客查询自己的车位成功").setData(stallCheck);
    }


    @ApiOperation("出租客分页查询自己全部车位列表")
    @GetMapping("/loadAllstalls")
    public Result<PageInfo> loadAllstalls(@RequestHeader String token, LoadAllstallsForm laf){
        String id = JwtUtil.parseUserToken(token).get("id").toString();
        System.out.println(id);
        LoadAllAccountParam param = BeanUtil.toBean(laf, LoadAllAccountParam.class);
        PageInfo<StallCheck> pageInfo =stallInfoService.loadAllstalls(param,id);
        return Result.getResult(PageInfo.class).setMessage("出租客分页查询自己全部车位列表成功").setData(pageInfo);
    }

    @ApiOperation("出租客查询自己车位状态")
    @GetMapping("/loadcarportstatebyid")
    @ApiImplicitParam(name = "stallInfoId",value = "车位信息id")
    public Result<StallInfo> loadcarportstatebyid(Integer stallInfoId){
        StallInfo stallInfo =stallInfoService.loadcarportstatebyid(stallInfoId);

        return Result.getResult(StallInfo.class).setCode(200).setMessage("出租客查询自己车位状态成功").setData(stallInfo);
    }

    @ApiOperation("用户根据物业ID（附近）和时间段查询可用车位")
    @PostMapping("/userGetCanUseStallsByTime")
    public Result<List<StallInfoDTO>> userGetCanUseStallsByTime(@RequestBody UserGetStallsByTimeForm form){
        List<StallInfoDTO> dtos =stallInfoService.userGetCanUseStallsByTime(BeanUtil.toBean(form,UserGetStallsByTimeParam.class));
        return Result.getListResult(StallInfoDTO.class).setMessage("查询可用车位成功").setData(dtos);
    }


    @ApiOperation("用户根据物业id查询上架车位")
    @PostMapping("/selectStallRentByCertifiedId")
    public Result selectStallRentByCertifiedId(SelectStallRentByCertifiedIdForm ssf){
        SelectStallRentByCertifiedIdParam param = BeanUtil.toBean(ssf, SelectStallRentByCertifiedIdParam.class);
        PageInfo<StallInfo> pageInfo = stallInfoService.selectStallRentByCertifiedId(param);
        return Result.getResult(PageInfo.class).setData(pageInfo).setMessage("查询上架车位成功");
    }


}

